using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
using System.Diagnostics;

namespace worktime
{
	static class Program
	{
		public static int Version = 153;

        public static string WorkTimeName = @"WorkTime";
		public static string WorkTimeRegistryHome = @"SOFTWARE\" + WorkTimeName;
		public static string WorkTimeRegistryAutorun = @"Software\Microsoft\Windows\CurrentVersion\Run";

		public static FormMain formMain;
		public static FormAbout formAbout;
		public static FormOptions frmOptions;

		public static ErrorHandler errors;
		public static DBWorker db;
		public static WorkTimeHandler workTimeHandler;
        public static OleDbConnection OldConnection;
		public static OleDbConnection Connection;
		public static bool UpdateInProgress = false;

		public static Object ErrorShownSaver = new object();
		public static bool ErrorIsShown = false;

        private static string BaseJetConnectingString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
        public static string DatabaseFileName = @"worktime.mdb";

#if DEBUG
        public static string OldDatabaseFullFileName = Path.Combine(@"C:\Program Files (x86)\WorkTime", DatabaseFileName);
        public static string DatabaseFullFileName = Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "WorkTime"), DatabaseFileName);
#else
        public static string OldDatabaseFullFileName = Path.Combine(Application.StartupPath, DatabaseFileName);
        public static string DatabaseFullFileName = Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "WorkTime"), DatabaseFileName);
#endif

        public static string OldConnectionString = BaseJetConnectingString + OldDatabaseFullFileName;
        public static string ConnectionString = BaseJetConnectingString + DatabaseFullFileName;

        class EmptyContext : ApplicationContext
        {
            public EmptyContext()
            {
                this.ExitThread();
            }
        }

		class MyAppContext : ApplicationContext
		{
			public MyAppContext()
			{
                OldConnection = new OleDbConnection(OldConnectionString);
				Connection = new OleDbConnection(ConnectionString);
				
				formAbout = new FormAbout();
				frmOptions = new FormOptions();
				errors = new ErrorHandler();
				db = new DBWorker();
				workTimeHandler = new WorkTimeHandler();

				formMain = new FormMain();
			}
		}

		[STAThread]
		static void Main(string[] args)
		{
            if (args.Length > 0 && args[0] == "INSTALLER")
            {
                Process.Start(Path.Combine(Application.StartupPath, "worktime.exe"));
                return;
            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            
            try
            {
                Application.Run(new MyAppContext());
            }
            catch (ExitException)
            {
                Program.errors.ProcessNotSeriousError("Program needs to shut down!");
            }
		}
	}
}